﻿/*
 *	Copyright (c) 2009 Queensland University of Technology. All rights reserved.
 *	The QUT Bioinformatics Collection is open source software released under the 
 *	Microsoft Public License (Ms-PL): http://www.microsoft.com/opensource/licenses.mspx.
 */
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QUT.Bio.Util;

namespace DBLoader {
	/// <summary>
	/// Interaction logic for ConnectionChooser.xaml
	/// </summary>
	public partial class BareGenomeChooser: Window {
		public bool initCalled;

		public BareGenomeChooser () {
			InitializeComponent();
		}

		/// <summary>
		/// Initialise this dialog.
		/// </summary>
		/// <param name="dataLayer"></param>
		/// <param name="logWriter"></param>
		
		public void Init ( DataLayer dataLayer, ILogWriter logWriter ) {
			initCalled = true;
			
			List<GenomeDescriptor> genomes = new List<GenomeDescriptor>();

			dataLayer.ListAvailableGenomes( delegate( string accessionVersion, string definition, int geneCount ) {
				genomes.Add( new GenomeDescriptor {
					AccessionVersion = accessionVersion,
					Definition = definition.Replace( ", complete genome", "" ).Replace( ", complete sequence", "" ),
					GeneCount = geneCount
				} );
			} );

			genomes.Sort( delegate( GenomeDescriptor x, GenomeDescriptor y ) {
				return x.Definition.CompareTo( y.Definition );
			} );

			genomeList.ItemsSource = genomes;
		}

		private bool Validate ( string fieldName, TextBox field, ref double variable ) {
			double valueEntered = 0;

			if ( !double.TryParse( field.Text.Trim(), out valueEntered ) ) {
				Alert.Show( "Invalid numeric data for {0}.", fieldName );
				return false;
			}

			if ( valueEntered < 0 ) {
				Alert.Show( "{0} must be greater than or equal to 0", fieldName );
				return false;
			}

			variable = valueEntered;
			return true;
		}

		private void Ok ( object sender, RoutedEventArgs e ) {
			DialogResult = true;
		}

		private void Cancel ( object sender, RoutedEventArgs e ) {
			DialogResult = false;
		}

		#region Property: SelectedGenomes
		/// <summary>
		/// Gets a list of selected genomes.
		/// </summary>

		public IEnumerable<GenomeDescriptor> SelectedGenomes {
			get {
				var t = ( from object x in genomeList.SelectedItems
						  select (GenomeDescriptor) x );
				return t;
			}
			set {
				genomeList.SelectedItems.Clear();

				foreach ( GenomeDescriptor genome in value ) {
					genomeList.SelectedItems.Add( genome );
				}
			}
		} 
		#endregion

		#region Method: show.
		/// <summary>
		/// Replace the Show method so we can check to ensure that Init has been called before displaying the window.
		/// </summary>

		public new void Show () {
			if ( !initCalled ) {
				throw new Exception( "Call Init before Show!" );
			}
			base.Show();
		} 
		#endregion

		#region Method: ShowDialog
		/// <summary>
		/// Replace ShowDialog so we can check that the Init method has been called before actually displaying the window.
		/// </summary>
		/// <returns></returns>

		public new bool? ShowDialog () {
			if ( !initCalled ) {
				throw new Exception( "Call Init before Show!" );
			}

			return base.ShowDialog();
		} 
		#endregion

		private class DatabaseInfo {
			public string Name {
				get;
				set;
			}
			public int OrganismCount {
				get;
				set;
			}
		}
	}
}
